package com.jerry.base.thread;

import lombok.extern.slf4j.Slf4j;

/**
 * User: Jerry
 * Date: 2024/1/31
 * Time: 22:50
 * Description:
 */
@Slf4j
public class ThreadJoin {

    static class Big implements Runnable {

        private int count = 0;

        @Override
        public void run() {
            while (true) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                log.info("大哥吃了{}个包子", ++count);

                if (count == 10) {
                    log.info("大哥吃完了");
                    break;
                }
            }
        }
    }

    public static void main(String[] args) {
        Big big = new Big();
        Thread thread = new Thread(big);
        thread.start();

        int count = 0;
        while (true) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            log.info("小弟吃了{}个包子", ++count);

            if (count == 5) {
                log.info("小弟都吃了5个包子了，需要等等老大");
                try {
                    thread.join();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }

            if (count == 10) {
                log.info("小弟吃完了");
                break;
            }
        }
    }
}
